//
//  CellForSection1.m
//  ProjectA
//
//  Created by dllo on 16/1/20.
//  Copyright © 2016年 刘旺辉. All rights reserved.
//



#import "CellTableViewSection1.h"
#import "CellForSection1.h"

#import "ModelForSection1.h"

#import "NetWorkHandler.h"

@interface CellTableViewSection1 ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *arrForModel;
@end

@implementation CellTableViewSection1

- (void)dealloc {
    
    self.collectionView.dataSource = nil;
    self.collectionView.delegate = nil;
    
    [_arrForModel release];
    [_collectionView release];
    [super dealloc];
}

// 重构初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self handleData];

        [self createCollectionView];
        
    }
    return self;
}

// 创建控件
- (void)createCollectionView {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width - 10 - 10 - 10) / 2.f, 235);
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 500) collectionViewLayout:flowLayout];
    
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.collectionView];
    
    [_collectionView release];
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.collectionView registerClass:[CellForSection1 class] forCellWithReuseIdentifier:@"pool"];
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    
    self.collectionView.frame = CGRectMake(0, 0, self.frame.size.width, 500);
    [self.collectionView reloadData];
    
}



#pragma mark - UITableViewDataSource
// 返回第一分区item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.arrForModel.count;
}

// 注册item
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
        
    CellForSection1 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    [cell passModel:[self.arrForModel objectAtIndex:indexPath.item]];
    return cell;
    
}

// 选中item进入下一界面
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"%ld", indexPath.item);
    
    ModelForSection1 *model = [self.arrForModel objectAtIndex:indexPath.row];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Data" object:nil userInfo:@{@"data":model}];
    
}


// 加载数据
- (void)handleData {
    self.arrForModel = [NSMutableArray array];
    [NetWorkHandler networkingWithUrl:@"http://api.breadtrip.com/v2/index/" comleteHandle:^(id result , NSData *data) {
        
        NSMutableArray *arrTemp10 = [NSMutableArray array];
        
        NSDictionary *dic10 = [result objectForKey:@"data"];
        NSArray *arr10 = [dic10 objectForKey:@"elements"];
        for (NSDictionary *dic in arr10) {
            
            NSNumber *number10 = [dic objectForKey:@"type"];
            if ([number10 isEqualToNumber:@(10)]) {
                
                [arrTemp10 addObject:dic];
            }
            
        }
        for (NSDictionary *dic in arrTemp10) {
            
            NSArray *arr10 = [dic objectForKey:@"data"];
            NSDictionary *dicTemp10 = [arr10 lastObject];
            ModelForSection1 *model10 = [[ModelForSection1 alloc] init];
            [model10 setValuesForKeysWithDictionary:dicTemp10];
        
            NSDictionary *dicUser10 = [dicTemp10 objectForKey:@"user"];
            [model10 setValuesForKeysWithDictionary:dicUser10];
            [self.arrForModel addObject:model10];
            [model10 release];
            
        }
        
        [self.collectionView reloadData];
        
        
        
    }];
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
